/*
Copyright (c) 2008 SmashedApples 

Contributors.  See: http://smashedapples.googlecode.com/

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package ullio.events
{    
    import flash.events.Event;
    
    import mx.events.DynamicEvent;
    import mx.utils.ObjectUtil;
    
    /**
     * 
     * @author brian holmes
     * 
     * 
     *  The AppEvent class allows you to dynamically dispatch events
     *  that are significant to the application.
     * 
     *  It subclasses DynamicEvent and it's important to note that the compiler
     *  can not help you catch run time events.
     * 
     *  It accepts one object of any type called args. It will loop through the args
     *  and set them on the object. This reduces dependencies on passing arguments because
     *  you don't have to  type the arguments on the event and can have your event handlers
     *  look for and modify your executing code passed on the arguments that are passed in.
     * 
     * 
     * 
     *   <p>Example:</p>
 	 *
 	 *  <pre>
 	 *  dispatchEvent( new AppEvent("SaveUser",{user:myUser}));
	 *  </pre>
	 * 	
	 *  <p>Event properties can be read as follows:</p>
	 *  <pre>
	 *   event.user 
	 *  </pre>
	 * 
     */   
     
      
  
    dynamic public class AppEvent extends DynamicEvent
    {    
        
        /**
         *  All properties on the args object are set on the AppEvent and can be accessed accordingly.
         *  In order to clone this event we always set and store the original arguments that were passed through.
         */        
        public var args : *;
        
        
        
        /**
         * 
         *  Contructor
         * 
         * @param type : String; The name of the event being passed through
         * @param args : *; The arguments that define the context of the event.
         * @param bubbles : Boolean; Whether or not the object Bubbles.
         * @param cancelable : Boolean; Whether or not the event is cancelable;
         * 
         */        
        public function AppEvent( type:String, args:*=null, bubbles:Boolean=true, cancelable:Boolean=false )
        {
           super( type, bubbles, cancelable );
           this.args = args;
           setEventContext(args);
        }
        
		
		/**
		 *  Sets the event context by looping over the args object 
		 *  and setting internal properties. 
		 * 
		 * @param args
		 * 
		 */		
		public function setEventContext(args:*=null):void
		{
			if(args == null)
                return void;
 			
 			for (var key:String in args)
            {
                if (key != "mx_internal_uid")
                    this[key] = args[key]; 
            } 
 		}
		
      	
      	
        override public function clone():Event
        {
        	return new AppEvent(type,args,bubbles,cancelable);
        }
     
	
              
    }
}